package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.SysAgentPermission;
import com.serkol.ams.entity.SysCategory;
import com.serkol.ams.mapper.SysCategoryMapper;
import com.serkol.ams.service.SysAgentPermissionService;
import com.serkol.ams.service.SysCategoryService;
import com.serkol.annotation.RedisCache;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【sys_category】的数据库操作Service实现
 * @createDate 2022-10-07 17:05:12
 */
@Service
public  class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory>
        implements SysCategoryService {
    @Autowired
    private SysAgentPermissionService sysAgentPermissionService;

    //  @Cacheable(cacheNames = "SysCategoryServiceImpl", key = "'fetchSysCategoryList::'+#language+'::type'+#type")
    @RedisCache(key = "SysCategory::mini::fetchSysCategoryList", isLang = true, isAuth = true, isAgent = true, isParam = true)
    @Override
    public List<Map<String, Object>> fetchSysCategoryList(int type) {
        //   long agentId = 1;
        LoginLog loginLog = AuthConstant.getLoginLog();

        LambdaQueryWrapper<SysAgentPermission> queryWrapperSystemPermission = new LambdaQueryWrapper<>();
        queryWrapperSystemPermission.select(SysAgentPermission::getCategoryId);
        queryWrapperSystemPermission.eq(SysAgentPermission::getAgentId, loginLog.getAgentId());
        List<Object> objects = sysAgentPermissionService.listObjs(queryWrapperSystemPermission);

        MyLambdaQueryWrapper<SysCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysCategory::getId, SysCategory::getAppid, SysCategory::getPage, SysCategory::getPath, SysCategory::getCol, SysCategory::isGrid, SysCategory::getSize);
        if (objects.size() > 0) {
            queryWrapper.in(SysCategory::getId, objects);
        }
        queryWrapper.select(SysCategory::getTargetType).as("targetType").langName(SysCategory::getName, SysCategory::getNameUg).as("name").picture(SysCategory::getImageId).as("image").select(SysCategory::getOtherId).as("otherId").select(SysCategory::isFlagIcon).as("flagIcon").select(SysCategory::isFlagTitle).as("flagTitle");
        queryWrapper.eq(SysCategory::getStatus, 0);
        if (type > 0) {
            queryWrapper.eq(SysCategory::getType, type);
        }
        queryWrapper.isNull(SysCategory::getParentId);
        //   queryWrapper.select(SysCategory::getId,SysCategory::getAppid,SysCategory::getDate,SysCategory::getName);
        List<Map<String, Object>> list = this.listMaps(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();


            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchSysCategoryParentList(id, objects);
            if (list1.size() > 0) {
                //  next.put("hideChildrenInSysCategory",true);
                //  next.put("hideInSysCategory",false);
            } else {
                // next.put("hideChildrenInSysCategory",false);
            }
            next.put("routes", list1);
            listMap.add(next);
        }
        System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKK::" + listMap.size() + "::" + listMap.toString());

        return listMap;
    }

    public List<Map<String, Object>> fetchSysCategoryParentList(Object parentId, List<Object> objects) {
        MyLambdaQueryWrapper<SysCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysCategory::getId, SysCategory::getAppid, SysCategory::getPage, SysCategory::getPath, SysCategory::getCol, SysCategory::isGrid, SysCategory::getSize);
        queryWrapper.select(SysCategory::getTargetType).as("targetType").langName(SysCategory::getName, SysCategory::getNameUg).as("name").picture(SysCategory::getImageId).as("image").select(SysCategory::getOtherId).as("otherId").select(SysCategory::isFlagIcon).as("flagIcon").select(SysCategory::isFlagTitle).as("flagTitle");
        if (objects.size() > 0) {
            queryWrapper.in(SysCategory::getId, objects);
        }
        queryWrapper.eq(SysCategory::getParentId, parentId);
        List<Map<String, Object>> list = this.listMaps(queryWrapper);

        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();

/*
            Object id = next.get("id");

            List<Map<String, Object>> list1 = fetchSysCategoryParentList(id);
            if (list1.size() > 0) {
                //  next.put("hideChildrenInSysCategory",true);
                //  next.put("hideInSysCategory",false);
            } else {
                // next.put("hideChildrenInSysCategory",false);
            }
            next.put("routes", list1);*/
            listMap.add(next);
        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> fetchSysCategoryTreeList() {
        MyLambdaQueryWrapper<SysCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysCategory::getId, SysCategory::getAppid, SysCategory::getDate, SysCategory::getName, SysCategory::getPage, SysCategory::getPath, SysCategory::getPermission, SysCategory::getSort, SysCategory::getStatus, SysCategory::getType, SysCategory::getCol, SysCategory::isGrid, SysCategory::getSize,
                        SysCategory::getParentId, SysCategory::getTargetType, SysCategory::getNameUg, SysCategory::getImageId, SysCategory::getOtherId, SysCategory::isFlagIcon, SysCategory::isFlagTitle).
                picture(SysCategory::getImageId).as("image");

        queryWrapper.isNull(SysCategory::getParentId);

        List<Map<String, Object>> list = this.listMaps(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        for (Map<String, Object> next : list) {
            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchSysCategoryTreeParentList(id);
            if (list1.size() > 0) {
                next.put("hideChildrenInSysCategory", true);
                next.put("hideInSysCategory", false);
                next.put("children", list1);
            } else {
                next.put("hideChildrenInSysCategory", false);
            }

            listMap.add(next);
        }
        return listMap;
    }

    public List<Map<String, Object>> fetchSysCategoryTreeParentList(Object parentId) {
        MyLambdaQueryWrapper<SysCategory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysCategory::getId, SysCategory::getAppid, SysCategory::getDate, SysCategory::getName, SysCategory::getPage, SysCategory::getPath, SysCategory::getPermission, SysCategory::getSort, SysCategory::getStatus, SysCategory::getType, SysCategory::getCol, SysCategory::isGrid, SysCategory::getSize);
        queryWrapper.select(SysCategory::getParentId).as("parentId").select(SysCategory::getTargetType).as("targetType").select(SysCategory::getNameUg).as("nameUg").select(SysCategory::getImageId).as("imageId").picture(SysCategory::getImageId).as("image").select(SysCategory::getOtherId).as("otherId").select(SysCategory::isFlagIcon).as("flagIcon").select(SysCategory::isFlagTitle).as("flagTitle");

        queryWrapper.eq(SysCategory::getParentId, parentId);


        List<Map<String, Object>> list = this.listMaps(queryWrapper);
        List<Map<String, Object>> listMap = new ArrayList<>();

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();

            Object id = next.get("id");
            List<Map<String, Object>> list1 = fetchSysCategoryTreeParentList(id);
            if (list1.size() > 0) {
                next.put("hideChildrenInSysCategory", true);
                next.put("hideInSysCategory", false);
                next.put("children", list1);
            } else {
                next.put("hideChildrenInSysCategory", false);
            }

            listMap.add(next);
        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> listSelect() {
        QueryWrapper<SysCategory> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().isNull(SysCategory::getParentId);
        queryWrapper1.select("NAME  as label,ID as value");
        List<Map<String, Object>> list = this.baseMapper.selectMaps(queryWrapper1);

        return list;
    }
    //byDelete

    @Override
    public boolean byDelete(long id) {
        if(id>0) {
            return this.baseMapper.deleteById(id) > 0;
        }
        return false;
    }

}




